/**
 * 对接lcd12864.c 实现游戏显示功能
 */
#include "lcd12864.c"
#include "game.h"

void display_init()
{
    lcd_init();
    lcd_clear_screan();
}

void display_game_start()
{
    lcd_display_words_in_page(2, 0, TETRIS_PIC, 128 * 32 / 8, 128, 32);
}

void display_game_over()
{
    lcd_display_words_in_page(0, 0, GAME_OVER_PIC, 128 * 64 / 8, 128, 64);
}

/**
 * 屏幕指定行和列，显示数字x
 */
void display_num(unsigned char page,
                 unsigned char col,
                 unsigned char num)
{
    // NUM_CHARS + num * 4  对应num在数组中元素的第一个byte的位置
    lcd_display_bytes_in_page(page, col, NUM_CHARS + num * 4, 4);
}

void display_speed_in_selection()
{
    display_num(4, 62, game_setting.speed);
}

/**
 * 游戏速度选择
 */
void display_speed_selection()
{
    lcd_clear_screan();
    lcd_display_same_bytes_in_page(0, 0, 0xFF, 128);
    lcd_display_same_bytes_in_page(7, 0, 0xFF, 128);
    // sizeof(SPEED_CHARS) / sizeof(SPEED_CHARS[0])
    // 对于char 数组 等价于
    lcd_display_bytes_in_page(3, 52, SPEED_CHARS, sizeof(SPEED_CHARS));
    lcd_display_bytes_in_page(4, 57, LEFT_ARROW, sizeof(LEFT_ARROW));
    lcd_display_bytes_in_page(4, 67, RIGHT_ARROW, sizeof(RIGHT_ARROW));
    display_speed_in_selection();
}

void display_score()
{
    unsigned char col = 3;
    display_num(1, col, game_setting.score / 10000);
    display_num(1, col += 5, (game_setting.score % 10000) / 1000);
    display_num(1, col += 5, (game_setting.score % 1000) / 100);
    display_num(1, col += 5, (game_setting.score % 100) / 10);
    display_num(1, col += 5, (game_setting.score % 10));
}

void display_speed_in_game()
{
    display_num(4, 13, game_setting.speed);
}

void display_init_game_page()
{
    unsigned char i;
    lcd_clear_screan();
    // SCORE
    lcd_display_bytes_in_page(0, 3, SCORE_CHARS, sizeof(SCORE_CHARS));
    // SPEED
    lcd_display_bytes_in_page(3, 3, SPEED_CHARS, sizeof(SPEED_CHARS));

    // 游戏区的两条竖线
    for (i = 0; i < 8; i++)
    {
        lcd_display_one_byte_in_page(i, 30, 0xFF);
        lcd_display_one_byte_in_page(i, 71, 0xFF);
    }

    // 右侧区域数据
    lcd_display_words_in_page(1, 85, LOGO_WORDS_CH,
                              sizeof(LOGO_WORDS_CH) / sizeof(LOGO_WORDS_CH[0]),
                              16, 16);
    lcd_display_words_in_page(3, 77, LOGO_WORDS_EN,
                              sizeof(LOGO_WORDS_EN) / sizeof(LOGO_WORDS_EN[0]),
                              8, 16);

    // 显示数字
    display_score();
    display_speed_in_game();
}

void display_pause()
{
    lcd_display_words_in_page(6, 81, PAUSE_CHARS,
                              sizeof(PAUSE_CHARS) / sizeof(PAUSE_CHARS[0]),
                              8, 16);
}

void display_pause_hide()
{
    lcd_display_same_bytes_in_page(6, 81, 0x00, 40);
    lcd_display_same_bytes_in_page(7, 81, 0x00, 40);
}
